import "../../../just/libafl-qemu-libpng.just"

FUZZER_NAME := "qemu_launcher"

HARNESS := TARGET_DIR / ("libpng-harness-" + PROFILE)

[unix]
build:
    cargo build \
      --profile {{ PROFILE }} \
      --features {{ ARCH }} \
      --target-dir {{ TARGET_DIR }}

[unix]
harness: libpng
    #!/bin/bash

    source {{ DOTENV }}

    $CROSS_CXX \
        ./harness.cc \
        $CROSS_CFLAGS \
        "{{ TARGET_DIR }}/build-png/.libs/libpng16.a" \
        "{{ TARGET_DIR }}/build-zlib/libz.a" \
        -I"{{ TARGET_DIR }}/build-png" \
        -I"{{ DEPS_DIR }}/libpng-1.6.37" \
        -I"{{ TARGET_DIR }}/build-zlib/zlib/lib" \
        -L"{{ TARGET_DIR }}/build-zlib/zlib/lib" \
        -o"{{ HARNESS }}" \
        -lm

[unix]
run: harness build
    #!/bin/bash

    source {{ DOTENV }}
    CUSTOM_LIBAFL_QEMU_ASAN_PATH={{ BUILD_DIR }}/$CROSS_TARGET/{{ PROFILE_DIR }}/libafl_qemu_asan_host.so \
    {{ FUZZER }} \
        --input ./corpus \
        --output {{ TARGET_DIR }}/output/ \
        --log {{TARGET_DIR}}/output/log.txt \
        --cores 0-7 \
        --asan-host-cores 0-3 \
        --cmplog-cores 2-5 \
        --iterations 1000000 \
        --tui \
        -- \
        {{ HARNESS }}


[unix]
test_inner: harness build
    #!/bin/bash

    source {{ DOTENV }}

    export QEMU_LAUNCHER={{ FUZZER }}

    ./tests/injection/test.sh || exit 1

    # complie again with simple mgr
    cargo build --profile={{PROFILE}} --features="simplemgr,{{ARCH}}" --target-dir={{ TARGET_DIR }} || exit 1

    export CUSTOM_LIBAFL_QEMU_ASAN_PATH={{ BUILD_DIR }}/$CROSS_TARGET/{{ PROFILE_DIR }}/libafl_qemu_asan_host.so
    ./tests/asan/host_test.sh || exit 1

    export CUSTOM_LIBAFL_QEMU_ASAN_PATH={{ BUILD_DIR }}/$CROSS_TARGET/{{ PROFILE_DIR }}/libafl_qemu_asan_guest.so
    ./tests/asan/guest_test.sh || exit 1

[unix]
test:
    ARCH=x86_64 just test_inner

single: harness build
    {{ FUZZER }} \
        --input ./corpus \
        --output {{ TARGET_DIR }}/output/ \
        --log {{ TARGET_DIR }}/output/log.txt \
        --cores 0 \
        -- \
        {{ HARNESS }}

asan_host: harness build
    #!/bin/bash

    source {{ DOTENV }}
    CUSTOM_LIBAFL_QEMU_ASAN_PATH={{ BUILD_DIR }}/$CROSS_TARGET/{{ PROFILE_DIR }}/libafl_qemu_asan_host.so \
    {{ FUZZER }} \
        --input ./corpus \
        --output {{ TARGET_DIR }}/output/ \
        --log {{ TARGET_DIR }}/output/log.txt \
        --cores 0 \
        --asan-host-cores 0 \
        -- \
        {{ HARNESS }}

asan_guest: harness build
    #!/bin/bash

    source {{ DOTENV }}
    CUSTOM_LIBAFL_QEMU_ASAN_PATH={{ BUILD_DIR }}/$CROSS_TARGET/{{ PROFILE_DIR }}/libafl_qemu_asan_guest.so \
    {{ FUZZER }} \
        --input ./corpus \
        --output {{ TARGET_DIR }}/output/ \
        --log {{ TARGET_DIR }}/output/log.txt \
        --cores 0 \
        --asan-guest-cores 0 \
        -- \
        {{ HARNESS }}

[unix]
clean:
    cargo clean
